package com.xxxx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.common.result.BaseResult;
import com.xxxx.common.utils.JsonUtil;
import com.xxxx.manager.mapper.GoodsMapper;
import com.xxxx.manager.pojo.Goods;
import com.xxxx.manager.pojo.GoodsCategory;
import com.xxxx.manager.pojo.GoodsExample;
import com.xxxx.manager.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.util.HtmlUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class GoodsServiceimpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;



    //商品新增实现类

    //商品保存
    @Override
    public BaseResult saveGoods(Goods goods) {
        //如果前端传来的goods已有id值，则不能再次添加
        if (null!=goods.getGoodsId()){
            return BaseResult.error();
        }
        //文本转义
        if(!StringUtils.isEmpty(goods.getGoodsContent())){
            goods.setGoodsContent(HtmlUtils.htmlEscape(goods.getGoodsContent(),"UTF-8"));

        }
        int result=goodsMapper.insertSelective(goods);
        BaseResult baseResult=BaseResult.error();
        if (null!=goods.getGoodsId()){
            baseResult=BaseResult.success();
            baseResult.setMessage(String.valueOf(goods.getGoodsId()));
            redisTemplate.delete("goodslistForPage01");
        }else {
            baseResult=BaseResult.error();

        }
        return baseResult ;
    }

    //分页查询
    @Override
    public BaseResult goodslistForPage01(Goods goods,Integer pageNum,Integer pageSize) {
        BaseResult baseResultJson=(BaseResult) redisTemplate.opsForValue().get("goodslistForPage01");

        if (!StringUtils.isEmpty(baseResultJson)) {
            return baseResultJson;
        }
        //构建分页对象
        PageHelper.startPage(pageNum,pageSize);
        System.out.println(goods);
        //构建查询条件
        GoodsExample example=new GoodsExample();
        GoodsExample.Criteria criteria=example.createCriteria();
        //分类参数
        if(null!=goods.getCatId() && 0!=goods.getCatId()){
            criteria.andCatIdEqualTo(goods.getCatId());
        }
        //品牌参数
        if (null!=goods.getBrandId() && 0!=goods.getBrandId()){
            criteria.andBrandIdEqualTo(goods.getBrandId());
        }
        //关键词
        if (!StringUtils.isEmpty(goods.getGoodsName())){
            criteria.andGoodsNameLike("%"+goods.getGoodsName()+"%");
        }
        List<Goods> list= goodsMapper.selectByExample(example);
        System.out.println(list);
        //判断查询结果是否为空,不为空返回BaseResult对象
        if (!CollectionUtils.isEmpty(list)){
            PageInfo<Goods> pageInfo=new PageInfo<>(list);

            BaseResult baseResult=BaseResult.success(pageInfo);
            redisTemplate.opsForValue().set("goodslistForPage01",baseResult,1, TimeUnit.DAYS);

            return baseResult;
        }
        return BaseResult.error();
    }
}
